package demo.booleanteeth.rxjavademo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;



import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;


public class MainActivity extends AppCompatActivity {

    private static final String TAG = "zd";
    private String tag = "zd";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//        Observable.just(1, 2, 3, 4)
//                .subscribeOn(Schedulers.io()) // 指定 subscribe() 发生在 IO 线程
//                .observeOn(AndroidSchedulers.mainThread()) // 指定 Subscriber 的回调发生在主线程
//                .subscribe(new Action1<Integer>() {
//                    @Override
//                    public void call(Integer number) {
//                        Log.d(tag, "number:" + number);
//                    }
//                });

//        Student s1 = new Student("消防1", 25);
//        ArrayList<Course> courses = new ArrayList<>();
//        courses.add(new Course("语文",56));
//        courses.add(new Course("数学",60));
//        courses.add(new Course("英语",85));
//        ArrayList<Course> courses2 = new ArrayList<>();
//        ArrayList<Course> courses3 = new ArrayList<>();
//        s1.setCourses(courses);
//        Student s2 = new Student("消防2", 26);
//        courses.add(new Course("语文",53));
//        courses.add(new Course("数学",89));
//        courses.add(new Course("英语",80));
//        s2.setCourses(courses2);
//        Student s3 = new Student("消防3", 27);
//        courses.add(new Course("语文",24));
//        courses.add(new Course("数学",85));
//        courses.add(new Course("英语",24));
//        s3.setCourses(courses3);
//        Student[] students = {s1, s2, s3};
//        Subscriber<Student> subscriber = new Subscriber<Student>() {
//            @Override
//            public void onCompleted() {
//            }
//            @Override
//            public void onError(Throwable e) {
//            }
//            @Override
//            public void onNext(Student student) {
//                List<Course> courses = student.getCourses();
//                for (int i = 0; i < courses.size(); i++) {
//                    Course course = courses.get(i);
//                    Log.d(tag, course.getName());
//                }
//            }
//        };
//        Observable.from(students)
//                .subscribe(subscriber);
//        Subscriber<Course> subscriber = new Subscriber<Course>() {
//            @Override
//            public void onCompleted() {
//
//            }
//
//            @Override
//            public void onError(Throwable e) {
//
//            }
//
//            @Override
//            public void onNext(Course course) {
//                Log.d(tag, course.getName());
//            }
//        };
//        Observable.from(students)
//                .flatMap(new Func1<Student, Observable<Course>>() {
//                    @Override
//                    public Observable<Course> call(Student student) {
//                        return Observable.from(student.getCourses());
//                    }
//                })
//                .subscribe(subscriber);
        Observable.create(new ObservableOnSubscribe<Integer>() {
            @Override
            public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
                Log.d(TAG, "emit 1");
                emitter.onNext(1);
                Log.d(TAG, "emit 2");
                emitter.onNext(2);
                Log.d(TAG, "emit 3");
                emitter.onNext(3);
                Log.d(TAG, "emit complete");
                emitter.onComplete();
                Log.d(TAG, "emit 4");
                emitter.onNext(4);
            }
        }).subscribe(new Observer<Integer>() {
            private Disposable mDisposable;
            private int i;
            @Override
            public void onSubscribe(Disposable d) {
                Log.d(TAG, "subscribe");
                mDisposable = d;
            }
            @Override
            public void onNext(Integer value) {
                Log.d(TAG, "onNext: " + value);
                i++;
                if (i == 2) {
                    Log.d(TAG, "dispose");
                    mDisposable.dispose();
                    Log.d(TAG, "isDisposed : " + mDisposable.isDisposed());
                }
            }
            @Override
            public void onError(Throwable e) {
                Log.d(TAG, "error");
            }
            @Override
            public void onComplete() {
                Log.d(TAG, "complete");
            }
        });
    }
}
